package com.hamny.gwt.webrtc.client;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.hamny.gwt.media.client.MediaStream;

public class RTCPeerConnection extends JavaScriptObject {

	protected RTCPeerConnection() {

	}

	public final native void createOffer(
			RTCSessionDescriptionCallback successCallback,
			RTCPeerConnectionErrorCallback failureCallback,
			MediaConstraints constraints)
	/*-{
		if (successCallback != null) {
			var onSuccess = function(desc) {
				successCallback.@com.hamny.gwt.webrtc.client.RTCSessionDescriptionCallback::callback(*)(desc);
			}
			if (failureCallback != null) {
				var onFailure = function(error) {
					failureCallback.@com.hamny.gwt.webrtc.client.RTCPeerConnectionErrorCallback::callback(*)(error);
				}
				if (constraints != null) {
					this.createOffer(onSuccess, onFailure, constraints);
				} else {
					this.createOffer(onSuccess, onFailure);
				}
			} else {
				this.createOffer(onSuccess);
			}
		}
	}-*/;

	public final native void createAnswer(
			RTCSessionDescriptionCallback successCallback,
			RTCPeerConnectionErrorCallback failureCallback,
			MediaConstraints constraints)
	/*-{
		if (successCallback != null) {
			var onSuccess = function(desc) {
				successCallback.@com.hamny.gwt.webrtc.client.RTCSessionDescriptionCallback::callback(*)(desc);
			}
			if (failureCallback != null) {
				var onFailure = function(error) {
					failureCallback.@com.hamny.gwt.webrtc.client.RTCPeerConnectionErrorCallback::callback(*)(error);
				}
				if (constraints != null) {
					this.createAnswer(onSuccess, onFailure, constraints);
				} else {
					this.createAnswer(onSuccess, onFailure);
				}
			} else {
				this.createAnswer(onSuccess);
			}
		}
	}-*/;

	public final native void setLocalDescription(
			RTCSessionDescription description, Callback successCallback,
			RTCPeerConnectionErrorCallback failureCallback)
	/*-{
		if (successCallback != null && failureCallback != null) {
			var onSuccess = function() {
				successCallback.@com.hamny.gwt.webrtc.client.Callback::callback()();
			}
			var onFailure = function(error) {
				failureCallback.@com.hamny.gwt.webrtc.client.RTCPeerConnectionErrorCallback::callback(*)(error);
			}
			this.setLocalDescription(description, onSuccess, onFailure);
		} else {
			this.setLocalDescription(description);
		}
	}-*/;

	public final native RTCSessionDescription getLocalDescription()
	/*-{
		return this.localDescription;
		;
	}-*/;

	public final native void setRemoteDescription(
			RTCSessionDescription description, Callback successCallback,
			RTCPeerConnectionErrorCallback failureCallback)
	/*-{
		if (successCallback != null && failureCallback != null) {
			var onSuccess = function() {
				successCallback.@com.hamny.gwt.webrtc.client.Callback::callback()();
			}

			var onFailure = function(error) {
				failureCallback.@com.hamny.gwt.webrtc.client.RTCPeerConnectionErrorCallback::callback(*)(error);
			}
			this.setRemoteDescription(description, onSuccess, onFailure);
		} else {
			this.setRemoteDescription(description);
		}
	}-*/;

	public final native RTCSessionDescription getRemoteDescription()
	/*-{
		return this.remoteDescription;
	}-*/;

	public final native String getSignalingState()
	/*-{
		return this.signalingState;
	}-*/;

	public final native void updateIce(RTCConfiguration configuration,
			MediaConstraints constraints)
	/*-{
		this.updateIce(configuration, constraints);
	}-*/;

	public final native void updateIce(RTCConfiguration configuration)
	/*-{
		this.updateIce(configuration);
	}-*/;

	public final native void addIceCandidate(RTCIceCandidate candidate,
			Callback successCallback,
			RTCPeerConnectionErrorCallback failureCallback)
	/*-{
		if (successCallback != null && failureCallback != null) {
			var onSuccess = function() {
				successCallback.@com.hamny.gwt.webrtc.client.Callback::callback()();
			}

			var onFailure = function(error) {
				failureCallback.@com.hamny.gwt.webrtc.client.RTCPeerConnectionErrorCallback::callback(*)(error);
			}

			this.addIceCandidate(candidate, onSuccess, onFailure);
		} else {
			this.addIceCandidate(candidate);
		}
	}-*/;

	public final native String getIceGatheringState()
	/*-{
		return this.iceGatheringState;
	}-*/;

	public final native String getIceConnectionState()
	/*-{
		return this.iceConnectionState;
	}-*/;

	public final native JsArray<MediaStream> getLocalStreams()
	/*-{
		return this.getLocalStreams();
	}-*/;

	public final native JsArray<MediaStream> getRemoteStreams()
	/*-{
		return this.getRemoteStreams();
	}-*/;

	public final native void addStream(MediaStream stream,
			MediaConstraints constraints)
	/*-{
		this.addStream(stream, constraints);
	}-*/;

	public final native void addStream(MediaStream stream)
	/*-{
		this.addStream(stream);
	}-*/;

	public final native void removeStream(MediaStream stream)
	/*-{
		this.removeStream(stream);
	}-*/;

	public final native void close()
	/*-{
		this.close();
	}-*/;

	public final native void setRTCPeerConnectionListener(
			RTCPeerConnectionEventHandler listener)
	/*-{
		this.onnegotiationneeded = function() {
			listener.@com.hamny.gwt.webrtc.client.RTCPeerConnectionEventHandler::onNegotiationNeeded()();
		}

		this.onicecandidate = function(event) {
			listener.@com.hamny.gwt.webrtc.client.RTCPeerConnectionEventHandler::onIceCandidate(*)(event);
		}

		this.onsignalingstatechange = function() {
			listener.@com.hamny.gwt.webrtc.client.RTCPeerConnectionEventHandler::onSignalingStateChange()();
		}

		this.onaddstream = function(event) {
			listener.@com.hamny.gwt.webrtc.client.RTCPeerConnectionEventHandler::onAddStream(*)(event);
		}

		this.onremovestream = function() {
			listener.@com.hamny.gwt.webrtc.client.RTCPeerConnectionEventHandler::onRemoveStream()();
		}

		this.oniceconnectionstatechange = function() {
			listener.@com.hamny.gwt.webrtc.client.RTCPeerConnectionEventHandler::onIceConnectionStateChange()();
		}
	}-*/;

}
